<template>
	<view class="container">
		<view class="rich-content" v-html="richTextContent"></view>
		<view class="countdown">
			<!-- 	<navigator url="/pages/form/form"> -->
			<button v-if="countdown>0"> {{ countdown  }} 秒后自动跳转</button>
			<!-- 	</navigator> -->
		</view>
	</view>
</template>

<script>
	import config from '@/config'
	import {
		getConfig
	} from '@/api/business/index.js'

	const fileprewurl = config.baseFileViewUrl
	export default {
		data() {
			return {
				countdown: 0,
				richTextContent: '<p>正在加载内容...</p>',
				timer: null,
				userId: '', // 存储URL中的userId
				parentUserId: '' // 存储URL中的parentUserId

			}
		},
		onLoad(option) {

			this.userId = option.userId || '';
			this.parentUserId = option.parentUserId || '';
			// 模拟从后台获取富文本内容
			this.loadRichContent();

		},
		onUnload() {
			// 页面卸载时清除定时器
			if (this.timer) {
				clearInterval(this.timer);
			}
		},
		onReachBottom() {
			// 页面上拉触底事件的处理函数
		},
		onPullDownRefresh() {
			// 页面下拉刷新事件的处理函数
			this.loadRichContent();
			uni.stopPullDownRefresh();
		},
		methods: {
			loadRichContent() {
				getConfig().then(response => {
					if (response.code == 200) {
						this.richTextContent = this.replaceImagePaths(response.data.noticeContent)
					} else {

					}
					console.log(response)
					this.countdown = 3
					// 启动倒计时
					this.startCountdown();
				})
			},
			replaceImagePaths(content, baseURL = config.baseFileViewUrl) {
				// 匹配所有以 /dev-api/profile/upload/ 开头的相对路径
				const regex = /src="(\/prod-api\/profile\/upload\/[^"]+)"/g;
				// 替换为完整路径
				return content.replace(regex, (match, path) => {
					return `src="${baseURL}${path}"`;
				});
			},
			startCountdown() {
				this.timer = setInterval(() => {
					this.countdown--;
					if (this.countdown <= 0) {
						clearInterval(this.timer);
						// 自动跳转到表单页面
						if (!this.userId) {
							uni.showToast({
								title: '获取用户信息失败，请重新扫码',
								icon: 'error',
								duration: 1000
							});
						} else {
							uni.navigateTo({
								url: `/pages/form/form?userId=${this.userId}&parentUserId=${this.parentUserId}`
								// 注意：若表单页面路径是 /pages/form/form1111，需修改为对应路径
							})
						}



					}
				}, 1000);
			}
		}
	}
</script>

<style scoped>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
		position: relative;
		padding-bottom: 120rpx;
		/* 为底部按钮留出空间 */
	}

	.rich-content {
		background-color: #ffffff;
		padding: 30rpx;
		border-radius: 10rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.rich-content h2 {
		color: #333;
		font-size: 36rpx;
		margin-bottom: 20rpx;
	}

	.rich-content p {
		color: #666;
		font-size: 28rpx;
		line-height: 1.6;
		margin-bottom: 20rpx;
	}

	.rich-content img {
		max-width: 100%;
		height: auto;
		margin: 20rpx 0;
	}

	.rich-content ul {
		padding-left: 40rpx;
	}

	.rich-content li {
		color: #666;
		font-size: 28rpx;
		line-height: 1.6;
		margin-bottom: 10rpx;
	}

	.countdown {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		text-align: center;
		margin-top: 40rpx;
		padding: 20rpx;
		background-color: #ffffff;
		box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.countdown text {
		font-size: 32rpx;
		color: #999;
	}

	.countdown button {
		background-color: #007aff;
		color: white;
		border: none;
		padding: 20rpx 40rpx;
		border-radius: 10rpx;
		font-size: 32rpx;
	}
</style>
